<?php
 
class DB_Functions {
 
    private $db;
 
    //put your code here
    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }
 
    // destructor
    function __destruct() {
 
    }
 
    /**
     * Get user by email and password
     */
    public function getUserByEmailAndPassword($email, $password) {
        $result = mysql_query("SELECT * FROM user WHERE login_id = '$email'") or die(mysql_error());
        // check for result
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);
            $salt = $result['salt'];
            $encrypted_password = $result['pwd'];
            $hash = md5($password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                return $result;
            }
        } else {
            // user not found
            return false;
        }
    }
 
    /**
     * Check user-route is existing or not
     */
    public function isRouteExisted($uid) {
        $result = mysql_query("SELECT user_id from user_node WHERE user_id = '$uid'");
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            // user existed
            return true;
        } else {
            // user not existed
            return false;
        }
    }

	/**
     * Storing new route
     * returns route details
     */
    public function storeRoute($src_lat, $src_lon, $src_name, $des_lat, $des_lon, $des_name, $via_lat, $via_lon, $via_name, $user_id) {
        
        $result = mysql_query("INSERT INTO node(src_lat, src_lon, dest_lat, dest_lon, src_name, dest_name, via_lat, via_lon, via_name) VALUES('$src_lat', '$src_lon', '$des_lat', '$des_lon', '$src_name', '$des_name', '$via_lat', '$via_lon','$via_name')");
        // check for successful store
        if ($result) {
            // get user details
            $uid = mysql_insert_id(); // last inserted id
		$res= mysql_query("INSERT INTO user_node(user_id,node_id) VALUES('$user_id','$uid')") or die(mysql_error());
		
            $result = mysql_query("SELECT * FROM node WHERE node_id = $uid");
            // return user details
            return mysql_fetch_array($result);
        } else {
            return false;
        }
    }
 
 
}
 
?>
